<template>
    <!-- //隐患整改 -->
    <div class="app-container">
        <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch"
            label-width="68px">
            <el-form-item label="站点名称" prop="groupName">
                <el-input v-model="queryParams.groupName" placeholder="请输入站点名称" clearable
                    @keyup.enter.native="handleQuery" />
            </el-form-item>
            <el-form-item label="整改状态" prop="pitfallCorrection">
                <el-select v-model="queryParams.pitfallCorrection" placeholder="请选择整改状态" style="width: 100%">
                    <el-option label="已整改" value="Y"></el-option>
                    <el-option label="未整改" value="N"></el-option>
                </el-select>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
                <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
            </el-form-item>
        </el-form>
        <el-table v-loading="loading" :data="barrelList" @selection-change="handleSelectionChange">
            <el-table-column label="站点名称" align="center" prop="groupName" />
            <el-table-column label="巡检隐患" align="center" prop="inspectionContent" />
            <el-table-column label="发现时间" align="center" prop="updateTime" />
            <el-table-column label="发现人" align="center" prop="inspectionUserName" />
            <el-table-column label="现场图片" align="center" prop="sceneImgs">
                <template slot-scope="scope">
                    <div style="color: #1890ff;cursor: pointer;" @click="imgDanger(scope.row.sceneImgs, '现场图片')">查看图片
                    </div>
                </template>
            </el-table-column>
            <el-table-column label="已采取措施" align="center" prop="pitfallCorrectionContent" />
            <el-table-column label="整改后图片" align="center" prop="pitfallCorrectionImgs">
                <template slot-scope="scope">
                    <div style="color: #1890ff;cursor: pointer;"
                        @click="imgDanger(scope.row.pitfallCorrectionImgs, '整改后图片')">查看图片</div>
                </template>
            </el-table-column>
            <el-table-column label="整改状态" align="center" prop="pitfallCorrection">
                <template slot-scope="scope">
                    <div>{{ scope.row.pitfallCorrection == 'Y' ? '已整改' : '未整改' }}</div>
                </template>
            </el-table-column>
            <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
                <template slot-scope="scope">
                    <el-button v-if="scope.row.pitfallCorrection != 'Y'" size="mini" type="text"
                        @click="handlePoint(scope.row)">整改</el-button>
                </template>
            </el-table-column>
        </el-table>

        <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum"
            :limit.sync="queryParams.pageSize" @pagination="getList" />

        <!-- 隐患整改对话框 -->
        <el-dialog title="隐患整改" :visible.sync="pointOpen" width="600px" append-to-body>
            <el-form ref="form" :model="form" :rules="rules" label-width="110px">
                <el-form-item label="已采取措施" prop="pitfallCorrectionContent">
                    <el-input type="textarea" v-model="form.pitfallCorrectionContent" placeholder="请输入采取措施内容" />
                </el-form-item>
                <el-form-item label="整改后图片" prop="pitfallCorrectionImgs">
                    <image-upload v-model="form.pitfallCorrectionImgs" :limit="10" />
                </el-form-item>
            </el-form>
            <div slot="footer" style="text-align: center;">
                <el-button type="primary" @click="submitpoint">确 定</el-button>
                <el-button @click="pointOpen = false">取 消</el-button>
            </div>
        </el-dialog>

        <el-dialog :title="titleItem" :visible.sync="imgOpen" width="600px" append-to-body>
            <div>
                <div style="display: flex;flex-wrap: wrap;">
                    <image-preview style="margin: 5px;" :key="index" v-for="(item, index) of imgData"
                        :src="baseApi + item" :width="120" :height="120"></image-preview>
                </div>
            </div>
            <div slot="footer" style="text-align: center;">
                <el-button @click="imgOpen = false">取 消</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
import { listRecord, getRecord, addRecord, updateRecord, delRecord } from "@/api/site/isRecord";
export default {
    name: "Barrel",
    dicts: ['sys_normal_disable', 'his_medicine_type', 'his_dept_status'],
    data() {
        return {
            baseApi: process.env.VUE_APP_BASE_API,
            imgOpen: false,
            imgData: [],
            pointOpen: false,
            pageSizes: [6, 10, 20, 30, 50],
            qrWidth: 70,
            qrHeight: 70,
            qrContent: "",
            // 遮罩层
            loading: false,
            // 选中数组
            ids: [],
            // 非单个禁用
            single: true,
            // 非多个禁用
            multiple: true,
            // 显示搜索条件
            showSearch: true,
            // 总条数
            total: 0,
            // 药桶编号表格数据
            barrelList: [],
            // 弹出层标题
            title: "",
            // 是否显示弹出层
            open: false,
            // 查询参数
            queryParams: {
                pageNum: 1,
                pageSize: 10,
                pitfallCorrection: null,
                groupName: null,
                pitfall: 'Y'
            },
            // 表单参数
            form: {
                checkedCities: []
            },
            // 表单校验
            rules: {
                pitfallCorrectionImgs: [
                    { required: true, message: "已采取措施不能为空", trigger: "blur" }
                ],
                pitfallCorrectionContent: [
                    { required: true, message: "整改图片不能为空", trigger: "blur" }
                ],

            },
            titleItem: ''
        };
    },
    created() {
        this.getList();
    },
    methods: {

        /** 查询药桶编号列表 */
        getList() {
            this.loading = true;
            listRecord(this.queryParams).then(response => {
                this.barrelList = response.rows;

                this.total = response.total;
                this.loading = false;
            });
        },
        // 取消按钮
        cancel() {
            this.open = false;
            this.reset();
        },
        // 表单重置
        reset() {
            this.form = {
                id: null,
                title: null,
                sort: null,
                createBy: null,
                createTime: null,
                updateBy: null,
                updateTime: null,
                dataStatus: null,
                delFlag: null
            };
            this.resetForm("form");
        },
        /** 搜索按钮操作 */
        handleQuery() {
            this.queryParams.pageNum = 1;
            this.getList();
        },
        /** 重置按钮操作 */
        resetQuery() {
            this.resetForm("queryForm");
            this.handleQuery();
        },
        // 多选框选中数据
        handleSelectionChange(selection) {
            this.ids = selection.map(item => item.id)
            this.single = selection.length !== 1
            this.multiple = !selection.length
        },
        /** 新增按钮操作 */
        handleAdd() {
            this.reset();
            this.open = true;

        },
        /** 修改按钮操作 */
        handleUpdate(row) {
            this.reset();
            const id = row.id || this.ids
            getRecord(id).then(response => {
                this.form = response.data;
                this.open = true;

            });
        },
        /** 提交按钮 */
        submitForm() {
            this.$refs["form"].validate(valid => {
                if (valid) {
                    if (this.form.id != null) {
                        updateRecord(this.form).then(response => {
                            this.$modal.msgSuccess("修改成功");
                            this.open = false;
                            this.getList();
                        });
                    } else {
                        addRecord(this.form).then(response => {
                            this.$modal.msgSuccess("新增成功");
                            this.open = false;
                            this.getList();
                        });
                    }
                }
            });
        },
        /** 删除按钮操作 */
        handleDelete(row) {
            const ids = row.id || this.ids;
            this.$modal.confirm('是否确认删除药桶编号编号为"' + ids + '"的数据项？').then(function () {
                return delRecord(ids);
            }).then(() => {
                this.getList();
                this.$modal.msgSuccess("删除成功");
            }).catch(() => { });
        },
        /** 导出按钮操作 */
        handleExport() {
            this.download('his/barrel/export', {
                ...this.queryParams
            }, `barrel_${new Date().getTime()}.xlsx`)
        },
        //整改
        handlePoint(row) {

            this.reset();
            const id = row.inspectionRecordId
            getRecord(id).then(response => {
                this.form = response.data;
                this.pointOpen = true
            });
        },
        submitpoint() {
            this.form.pitfallCorrection = 'Y'
            updateRecord(this.form).then(response => {
                this.$modal.msgSuccess("修改成功");
                this.pointOpen = false
                this.getList();
            });
        },

        imgDanger(item, title) {
            this.imgOpen = true
            this.titleItem = title
            if (item) {
                this.imgData = item.split(',')
            }

        }
    }
};
</script>
